
from reportlab.lib.units import cm
import reportlab.lib.pagesizes as pagesizes

import layout
import fb2parser

__builtins__.pagesizes = pagesizes
__builtins__.cm = cm
__builtins__.Default = layout.DefaultConfig

def myunichr(i):
    return unichr(i).encode('utf-8')

def printt(fb2in, pdfout, cfgf):
    cfg = __import__(cfgf)

    print 'Parsing...'

    ## HACK FIX for bug in reportlab.lib.xmllib
    oldchr = chr
    __builtins__.chr = myunichr

    meta, body, binaries = fb2parser.parse(fb2in, cfg.Config.enableimages)

    __builtins__.chr = oldchr

    print 'OK'

    layout.go(pdfout, meta, body, binaries, cfg.Config)

if __name__=='__main__':
    import sys
    if len(sys.argv) != 4:
        print 'Usage:', sys.argv[0], " <input fb2 file> <output pdf file> 'gui-config'"
    else:
        printt(sys.argv[1], sys.argv[2], sys.argv[3])
